package com.example.yang.yixin.Activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.example.yang.yixin.R;

import java.util.ArrayList;
import java.util.List;

public class TestActivity extends BaseActivity {
public LocationClient mlocationClient;
private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        mlocationClient=new LocationClient(getApplicationContext());
        textView=findViewById(R.id.position);
        mlocationClient.registerLocationListener((BDAbstractLocationListener) new MyLocationListener());
        List<String> permissionList=new ArrayList<>();
        if(ContextCompat.checkSelfPermission(TestActivity.this,Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
            permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
        if(ContextCompat.checkSelfPermission(TestActivity.this,Manifest.permission.READ_PHONE_STATE)!=PackageManager.PERMISSION_GRANTED){
            permissionList.add(Manifest.permission.READ_PHONE_STATE);
        }
        if(ContextCompat.checkSelfPermission(TestActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
            permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if(!permissionList.isEmpty()){
           String [] permissions=permissionList.toArray(new String[permissionList.size()]);
            ActivityCompat.requestPermissions(TestActivity.this,permissions,1);
        }else{
            requestLocation();
        }
    }

    private void requestLocation() {
        mlocationClient.start();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) {
    switch (requestCode){
        case 1:
            if(grantResults.length>0){
                for(int result:grantResults){
                    if(result!=PackageManager.PERMISSION_GRANTED){
                        Toast.makeText(getApplicationContext(),"必须同意所有权限才能使用该应用",Toast.LENGTH_SHORT).show();
                        finish();
                        return;
                    }
                }
                requestLocation();
            }else{
                Toast.makeText(getApplicationContext(),"发生未知错误",Toast.LENGTH_SHORT).show();
                finish();
            }
            break;
            default:
    }

    }

    private class MyLocationListener extends BDAbstractLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(final BDLocation bdLocation) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    StringBuilder currtPosition=new StringBuilder();
                    currtPosition.append("纬度:").append(bdLocation.getLatitude()).append("\n");
                    currtPosition.append("经度:").append(bdLocation.getLongitude()).append("\n");
                    currtPosition.append("定位方式:");
                    if(bdLocation.getLocType()==BDLocation.TypeGpsLocation){
                        currtPosition.append("GPS");
                    }
                    else if(bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){
                        currtPosition.append("网络");
                    }
                    textView.setText(currtPosition);
                }
            });
        }
    }
}
